package com.common.game;

import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import org.apache.log4j.Logger;

/**
 * User: wattsc5
 * <p/>
 * Date: 2/27/12
 */
public class Player
{
	private static final Logger logger = Logger.getLogger(Player.class);

	private String name;

	private int id;

	private SocketChannel socketChannel;

	private final List<Dice> diceList = new ArrayList<Dice>();

	private boolean isPlayersTurn;

	private Bid bid;

	private boolean challenge;

	private Player previousPlayer;

	private Player nextPlayer;

	private boolean isInGame;

	private int numberOfInvalidMoves;

	public Timer timeoutTimer;

	/**
	 * Initializes a new instance of a Player.
	 *
	 * @param name the name of the player.
	 * @param playerId the id for the player.
	 */
	public Player(String name, int playerId)
	{
		this.name = name;
		this.id = playerId;
		this.timeoutTimer = new Timer();
	}

	public Player getPreviousPlayer()
	{
		return previousPlayer;
	}

	public void setPreviousPlayer(Player previousPlayer)
	{
		this.previousPlayer = previousPlayer;
	}

	public int getNumberOfInvalidMoves()
	{
		return this.numberOfInvalidMoves;
	}

	public void addInvalidMove()
	{
		this.numberOfInvalidMoves++;
	}

	public boolean isInGame()
	{
		return isInGame;
	}

	public void setInGame(boolean inGame)
	{
		isInGame = inGame;
	}

	public void setNextPlayer(Player nextPlayer)
	{
		this.nextPlayer = nextPlayer;
	}

	public boolean isChallenge()
	{
		return challenge;
	}

	public void setChallenge(boolean challenge)
	{
		this.challenge = challenge;
	}

	public void setNewBid(Bid bid)
	{
		this.bid = bid;
	}

	public void setSocketChannel(SocketChannel socketChannel)
	{
		this.socketChannel = socketChannel;
	}

	public void addDie(int numberOfDieToAdd)
	{
		for (int i = 0; i < numberOfDieToAdd; i++)
		{
			this.diceList.add(new Dice());
		}
	}

	public void removeDie()
	{
		// TODO: Make sure this works.
		this.diceList.remove(0);
	}

	public boolean isPlayersTurn()
	{
		return isPlayersTurn;
	}

	public void setPlayersTurn(boolean playersTurn)
	{
		isPlayersTurn = playersTurn;
	}

	public SocketChannel getSocketChannel()
	{
		return socketChannel;
	}

	public int getId()
	{
		return id;
	}

	public String getName()
	{
		return name;
	}

	public String toString()
	{
		return "Name:" + this.getName() + " ID:" + this.getId() + " InGame:" + this.isInGame();
	}

	public void rollDice()
	{
		for (Dice die : this.diceList)
		{
			die.rollDice();
		}
	}

	public int getDiceCount()
	{
		return this.diceList.size();
	}

	public List<Dice> getDice()
	{
		return this.diceList;
	}

	public Player getNextPlayer()
	{
		return this.nextPlayer;
	}

	public Bid getBid()
	{
		return this.bid;
	}

	public Timer getNewTimeoutTimer()
	{
		this.timeoutTimer = new Timer();
		return this.timeoutTimer;
	}

	public void cancelTimeoutTimer()
	{
		this.timeoutTimer.cancel();
	}

	public void setNumberOfDie(int startingNumberOfDie)
	{
		this.diceList.clear();

		this.addDie(startingNumberOfDie);
	}

	public void setDiceValues(List<Integer> diceValues)
	{
		for (int i = 0; i < diceValues.size(); i++)
		{
			this.diceList.get(i).setValue(diceValues.get(i));
		}
	}
}
